package com.styytech.dadabao.test.camera;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.styytech.dadabao.DadaApplication;
import com.styytech.dadabao.R;
import com.styytech.dadabao.widge.headportrait.Bimp;
import com.styytech.dadabao.widge.headportrait.FileUtils;
import com.styytech.dadabao.widge.headportrait.GridAdapter;
import com.styytech.dadabao.widge.headportrait.PhotoActivity;
import com.styytech.dadabao.widge.headportrait.TestPicActivity;

import java.io.File;
import java.util.ArrayList;



public class GridCameraActivity extends Activity implements OnClickListener, OnDismissListener{
    GridView gridview_send_image;
    private GridAdapter adapter;
    private LinearLayout myView;
    private PopupWindow myWindow;
    private RelativeLayout pop_menu_background;
    private LinearLayout top_layout;
    private Button btn_load;
    // 存图片的存放地址
    private ArrayList<String> list_bitmap_path_upload = new ArrayList<String>();
    private static final int TAKE_PICTURE = 0x000000;

    private String path = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_camera_more);
        gridview_send_image=(GridView) findViewById(R.id.gridview_send_image);
        pop_menu_background = (RelativeLayout) findViewById(R.id.send_menu_background);
        top_layout=(LinearLayout)findViewById(R.id.top_layout);
        btn_load=(Button)findViewById(R.id.btn_load);
        btn_load.setOnClickListener(this);
        initView();
    }
    private void initView(){
        adapter = new GridAdapter(this);
        adapter.update();
        gridview_send_image.setAdapter(adapter);
        gridview_send_image.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                if (arg2 == Bimp.bmp.size()) {
                    //点击的是添加图片
                    shotSelectImages();
                } else {
                    Intent intent = new Intent(GridCameraActivity.this,
                            PhotoActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("ID", arg2);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            }
        });
    }
    public void shotSelectImages() {
        LayoutInflater inflater = LayoutInflater.from(this);
        myView = (LinearLayout) inflater.inflate(
                R.layout.select_images_from_local, null);

        myView.findViewById(R.id.btn_paizhao).setOnClickListener(this);
        myView.findViewById(R.id.btn_select_pic).setOnClickListener(this);
        myView.findViewById(R.id.btn_cancle).setOnClickListener(this);

        myWindow = new PopupWindow(myView,
                RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT, true);
        myWindow.setAnimationStyle(R.style.AnimationPopup);
        myWindow.setOutsideTouchable(true);
        myWindow.setBackgroundDrawable(new BitmapDrawable());
        myWindow.showAtLocation(top_layout, Gravity.BOTTOM, 0, 0);
        myWindow.setOnDismissListener(this);
        final Animation animation = (Animation) AnimationUtils.loadAnimation(
                this, R.anim.slide_in_from_bottom);
        myView.startAnimation(animation);
        pop_menu_background.setVisibility(View.VISIBLE);
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.btn_paizhao:
                myWindow.dismiss();
                startCamera();
                break;
            case R.id.btn_select_pic:
                myWindow.dismiss();
                String status = Environment.getExternalStorageState();
                if (status.equals(Environment.MEDIA_MOUNTED)) {
                    Intent intentFromGallery = new Intent(GridCameraActivity.this,
                            TestPicActivity.class);
                    startActivity(intentFromGallery);
                }
                break;
            case R.id.btn_cancle:
                myWindow.dismiss();
                break;
            case R.id.btn_load:
                for (int i = 0; i < Bimp.drr.size(); i++) {
                    String Str = Bimp.drr.get(i).substring(
                            Bimp.drr.get(i).lastIndexOf("/") + 1,
                            Bimp.drr.get(i).lastIndexOf("."));
                    Log.e("das", "path--------"+ FileUtils.SDPATH + Str + ".JPEG");
                    list_bitmap_path_upload.add(FileUtils.SDPATH + Str + ".JPEG");
                }
                break;
            default:
                break;
        }
    }
    //使用相机照相
    private void startCamera() {
        // TODO Auto-generated method stub
        //判断SD状态
        String status = Environment.getExternalStorageState();
        //SD卡正常挂载
        if (status.equals(Environment.MEDIA_MOUNTED)) {
            try {
                File file_sys = new File(
                        Environment.getExternalStorageDirectory() + "/myimage/");
                if (!file_sys.exists()) {
                    file_sys.mkdirs();
                }
                File file = new File(file_sys, String.valueOf(System
                        .currentTimeMillis()) + ".jpg");
                path = file.getPath();
                Uri imageUri = Uri.fromFile(file);
                Intent openCameraIntent = new Intent(
                        MediaStore.ACTION_IMAGE_CAPTURE);
                openCameraIntent.putExtra(MediaStore.Images.Media.ORIENTATION,
                        0);
                openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(openCameraIntent, TAKE_PICTURE);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(GridCameraActivity.this,
                        "没有找到储存目录",
                        Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(GridCameraActivity.this,
                    "没有储存卡",
                    Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // 结果码不等于取消时候
        if (resultCode != RESULT_CANCELED) {
            switch (requestCode) {
                case TAKE_PICTURE:
                    if (Bimp.drr.size() < DadaApplication.pci_numbers && resultCode == -1) {
                        Bimp.drr.add(path);
                    }
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
    @Override
    public void onDismiss() {
        // TODO Auto-generated method stub
        pop_menu_background.setVisibility(View.GONE);
    }
    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        if(adapter != null)
            adapter.update();
        super.onRestart();
    }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Bimp.bmp.clear();
        Bimp.drr.clear();
        list_bitmap_path_upload.clear();
        Bimp.max = 0;
        if(gridview_send_image != null)
            gridview_send_image.setAdapter(null);
        adapter = null;
    }

}

